<script setup>
import {onMounted, ref} from "vue";
import {useRoute} from "vue-router";
import axios from "axios";
import server from "../../config/server.js";
import storage from "../../util/storage.js";

const userId = ref()
const user = ref()
const userDetail = ref()
const route = useRoute()

const getUserDetail = () => {
  axios({
    method: "get",
    url: `${server.apiBaseUrl}/zdppy_amuserdetail?user_id=${userId.value}`
  }).then(resp => {
    console.log(resp.data.data.results)
    const data = resp.data.data.results
    if (data.length > 0) {
      userDetail.value = data[0]
      console.log("userDetail.value ", userDetail.value)
    } else {
      axios({
        method: "post",
        url: `${server.apiBaseUrl}/zdppy_amuserdetail`,
        contentType: "application/json",
        data: {
          user_id: user.value.id,
          ename: user.value.username,
        }
      }).then(resp => {
        console.log("add user detail ", resp.data)
        getUserDetail()
      })
    }
  })
}

onMounted(() => {
  console.log(route.query)
  userId.value = route.query.id
  console.log("userId", userId.value)
  getUserDetail()
  user.value = storage.get("user")
})
</script>
<template>
  <a-descriptions title="用户详情">
    <a-descriptions-item
        v-for="(v,k) in userDetail"
        :label="k"
        :key="k"
    >
      {{ v }}
    </a-descriptions-item>
  </a-descriptions>
</template>